import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionTest1 {
    @Test
    public void test1() {
        Collection c1 = new ArrayList();
        c1.add("AA");
        c1.add("BB");
        c1.add(128);
        c1.add(new Person("Jack",19));
        System.out.println(c1);
        System.out.println(c1.size());

        Collection c2 = new ArrayList();
        c2.add("CC");
        c2.add("AA");
        c2.add(55);

        //添加一个集合,如果不用addAll，则视为一个对象添加
        c1.addAll(c2);

        System.out.println(c1);
        System.out.println(c1.size());

        //移除元素,要移除一个集合，则用removeAll
        c1.remove("CC");
//        c1.removeAll(c2);
        System.out.println(c1);
        System.out.println(c1.size());

        //清空，底层逻辑是一个一个的删除，而不是简单设置长度为0
//        c1.clear();
//        System.out.println(c1);
//        System.out.println(c1.size());

        //查询
        System.out.println("是否包含AA：" +c1.contains("AA"));
        System.out.println("是否包含集合c2： " + c1.containsAll(c2));
        System.out.println("集合是否为空：" + c1.isEmpty());

        //转换
        System.out.println(c1);
        System.out.println(c2);
        //只有c2中有的元素才会被保留
        c1.retainAll(c2);
        System.out.println(c1);

        Object[] nb = c1.toArray();
        System.out.println(nb.length);

        //遍历，用hasNext判断是否有下一个元素，避免超出
        Iterator c1It = c1.iterator();
        while(c1It.hasNext()) {
            System.out.println(c1It.next());
        }

    }

}

class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person() {

    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
}
